package com.draven.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.draven.common.entity.Note;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 笔记Mapper接口
 * 
 * @author draven
 */
@Mapper
public interface NoteMapper extends BaseMapper<Note> {

    /**
     * 分页查询笔记列表（包含分类名称）
     * 
     * @param page 分页参数
     * @param title 标题关键字
     * @param categoryId 分类ID
     * @param status 状态
     * @return 笔记列表
     */
    @Select("<script>" +
            "SELECT n.*, c.category_name " +
            "FROM note n " +
            "LEFT JOIN note_category c ON n.category_id = c.id " +
            "WHERE n.del_flag = 0 " +
            "<if test='title != null and title != \"\"'>" +
            "AND n.title LIKE CONCAT('%', #{title}, '%') " +
            "</if>" +
            "<if test='categoryId != null'>" +
            "AND n.category_id = #{categoryId} " +
            "</if>" +
            "<if test='status != null'>" +
            "AND n.status = #{status} " +
            "</if>" +
            "ORDER BY n.is_top DESC, n.create_time DESC" +
            "</script>")
    IPage<Note> selectNotePageWithCategory(Page<Note> page, 
                                          @Param("title") String title,
                                          @Param("categoryId") Long categoryId,
                                          @Param("status") Integer status);

    /**
     * 根据标签ID查询笔记列表
     * 
     * @param page 分页参数
     * @param tagId 标签ID
     * @return 笔记列表
     */
    @Select("SELECT n.*, c.category_name " +
            "FROM note n " +
            "LEFT JOIN note_category c ON n.category_id = c.id " +
            "INNER JOIN note_tag_relation r ON n.id = r.note_id " +
            "WHERE n.del_flag = 0 AND r.del_flag = 0 AND r.tag_id = #{tagId} " +
            "ORDER BY n.is_top DESC, n.create_time DESC")
    IPage<Note> selectNotePageByTagId(Page<Note> page, @Param("tagId") Long tagId);

    /**
     * 查询笔记详情（包含分类名称）
     * 
     * @param id 笔记ID
     * @return 笔记详情
     */
    @Select("SELECT n.*, c.category_name " +
            "FROM note n " +
            "LEFT JOIN note_category c ON n.category_id = c.id " +
            "WHERE n.id = #{id} AND n.del_flag = 0")
    Note selectNoteDetailById(@Param("id") Long id);

    /**
     * 增加阅读次数
     * 
     * @param id 笔记ID
     * @return 影响行数
     */
    @Update("UPDATE note SET view_count = view_count + 1 WHERE id = #{id}")
    int incrementViewCount(@Param("id") Long id);

    /**
     * 增加点赞次数
     * 
     * @param id 笔记ID
     * @return 影响行数
     */
    @Update("UPDATE note SET like_count = like_count + 1 WHERE id = #{id}")
    int incrementLikeCount(@Param("id") Long id);

    /**
     * 查询最近的笔记
     * 
     * @param limit 限制数量
     * @return 笔记列表
     */
    @Select("SELECT n.*, c.category_name " +
            "FROM note n " +
            "LEFT JOIN note_category c ON n.category_id = c.id " +
            "WHERE n.del_flag = 0 AND n.status = 1 " +
            "ORDER BY n.create_time DESC LIMIT #{limit}")
    List<Note> selectRecentNotes(@Param("limit") Integer limit);

    /**
     * 查询热门笔记（按阅读量排序）
     * 
     * @param limit 限制数量
     * @return 笔记列表
     */
    @Select("SELECT n.*, c.category_name " +
            "FROM note n " +
            "LEFT JOIN note_category c ON n.category_id = c.id " +
            "WHERE n.del_flag = 0 AND n.status = 1 " +
            "ORDER BY n.view_count DESC, n.create_time DESC LIMIT #{limit}")
    List<Note> selectHotNotes(@Param("limit") Integer limit);

    /**
     * 分页查询笔记列表
     * 
     * @param page 分页参数
     * @param note 查询条件
     * @return 笔记列表
     */
    @Select("<script>" +
            "SELECT n.*, c.category_name " +
            "FROM note n " +
            "LEFT JOIN note_category c ON n.category_id = c.id " +
            "WHERE n.del_flag = 0 " +
            "<if test='note.title != null and note.title != \"\"'>" +
            "AND n.title LIKE CONCAT('%', #{note.title}, '%') " +
            "</if>" +
            "<if test='note.categoryId != null'>" +
            "AND n.category_id = #{note.categoryId} " +
            "</if>" +
            "<if test='note.status != null'>" +
            "AND n.status = #{note.status} " +
            "</if>" +
            "ORDER BY n.is_top DESC, n.create_time DESC" +
            "</script>")
    IPage<Note> selectNotePageList(Page<Note> page, @Param("note") Note note);

    /**
     * 根据标签ID分页查询笔记列表
     * 
     * @param page 分页参数
     * @param tagId 标签ID
     * @return 笔记列表
     */
    @Select("SELECT n.*, c.category_name " +
            "FROM note n " +
            "LEFT JOIN note_category c ON n.category_id = c.id " +
            "INNER JOIN note_tag_relation r ON n.id = r.note_id " +
            "WHERE n.del_flag = 0 AND r.del_flag = 0 AND r.tag_id = #{tagId} " +
            "ORDER BY n.is_top DESC, n.create_time DESC")
    IPage<Note> selectNotesByTagId(Page<Note> page, @Param("tagId") Long tagId);
}